package com.fs.redis.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
	private static final ObjectMapper MAPPER = new ObjectMapper();

	static {
		MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
				.setSerializationInclusion(Include.NON_NULL);
	}

	/**
	 * Java Object Maps To Json
	 */
	public static String toJson(Object obj) {
		String result;
		if (obj == null || obj instanceof String) {
			return (String) obj;
		}
		try {
			result = MAPPER.writeValueAsString(obj);
		} catch (Exception e) {
			LOGGER.error("Java Object Maps To Json Error !");
			throw new RuntimeException("Java Object Maps To Json Error !", e);
		}
		return result;
	}

}
